package by.gravity.provider;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import by.gravity.constants.DBConstants;
import by.gravity.constants.DbQueryConstants;
import by.gravity.helper.ConnectionHelper;
import by.gravity.model.Lesson;
import by.gravity.model.Week;

public class GrsuSheduleProvider implements DBConstants, SheduleProvider {

	@Override
	public List<String> getSpeciality() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		List<String> result = new ArrayList<String>();

		ResultSet resultSet = ConnectionHelper.getInstance().executeQuery(DbQueryConstants.getSpecialityQuery());

		while (resultSet.next()) {
			result.add(resultSet.getString(SPECIALITY_NAME_FIELD));
		}

		return result;
	}

	@Override
	public List<String> getKurs(String speciality) throws InstantiationException, IllegalAccessException, ClassNotFoundException,
			SQLException {
		List<String> result = new ArrayList<String>();

		ResultSet resultSet = ConnectionHelper.getInstance().executeQuery(DbQueryConstants.getKurs(speciality));

		while (resultSet.next()) {
			result.add(resultSet.getString(KURS_NAME_FIELD));
		}

		return result;
	}

	@Override
	public List<String> getGroups(String specialityName, String kursName) throws InstantiationException, IllegalAccessException,
			ClassNotFoundException, SQLException {
		List<String> result = new ArrayList<String>();

		ResultSet resultSet = ConnectionHelper.getInstance().executeQuery(DbQueryConstants.getGroups(specialityName, kursName));

		while (resultSet.next()) {
			result.add(resultSet.getString(GROUP_NAME_FIELD));
		}

		return result;
	}

	@Override
	public Week getPairs(String specialityName, String kursName, String groupName, long weekStart) throws SQLException,
			InstantiationException, IllegalAccessException, ClassNotFoundException {
		ResultSet resultSet = ConnectionHelper.getInstance().executeQuery(
				DbQueryConstants.getPairs(specialityName, kursName, groupName, weekStart));

		Week week = new Week();

		week.setSpecialityName(specialityName);
		week.setKursName(kursName);
		week.setGroupName(groupName);

		while (resultSet.next()) {
			Lesson lesson = week.newLesson();

			lesson.setAuditoryName(resultSet.getString(PAIR_AUDITORY_NAME));
			lesson.setTimeStart(resultSet.getString(PAIR_TIME_START));
			lesson.setTimeFinish(resultSet.getString(PAIR_TIME_FINISH));
			lesson.setTeacherName(resultSet.getString(PAIR_TEACHER_NAME));
			lesson.setLessonName(resultSet.getString(PAIR_LESSON_NAME));
			week.addLesson(resultSet.getDate(PAIR_DATE), lesson);
		}

		return week;
	}

}
